package com.example.service.impl;

import com.example.dao.UserDao;
import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User getUserById(Integer id) {
        return userDao.findById(id);
    }

    @Override
    public List<User> getAllUsers() {
        return userDao.findAll();
    }

    @Override
    public boolean addUser(User user) {
        return userDao.insert(user) > 0;
    }

    @Override
    public boolean updateUser(User user) {
        return userDao.update(user) > 0;
    }

    @Override
    public boolean deleteUser(Integer id) {
        return userDao.delete(id) > 0;
    }

    @Override
    public User getUserByNameAndPassword(String username, String password) {
        return this.userDao.findByNameAndPassword(username, password);
    }

    @Override
    public boolean isUsernameExists(String username) {
        return userDao.findByUsername(username) != null;
    }

    @Override
    public User login(String username, String password) {
        // 实际项目中应对密码加密后验证
        return  userDao.findByUsernameAndPassword(username, password);
    }


    // 其他方法...
    }
